/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_components/zhiqim_manager.htm
 *
 * Zhiqim Manager is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.manager.action;

import java.util.ArrayList;
import java.util.List;

import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.extend.StdSwitchAction;
import org.zhiqim.httpd.validate.ones.IsIntegerValue;
import org.zhiqim.httpd.validate.ones.IsLen;
import org.zhiqim.httpd.validate.onex.IsInteger;
import org.zhiqim.kernel.json.Jsons;
import org.zhiqim.kernel.util.Lists;
import org.zhiqim.kernel.util.Validates;
import org.zhiqim.manager.ZmrConstants;
import org.zhiqim.manager.dao.ZmrDeptDao;
import org.zhiqim.manager.dao.ZmrOperatorDao;
import org.zhiqim.manager.dbo.ZmrDept;
import org.zhiqim.manager.dbo.ZmrOperator;
import org.zhiqim.orm.ORM;
import org.zhiqim.orm.dbo.Selector;

/**
 * 部门管理
 *
 * @version v1.0.0 @author zouzhigang 2015-5-29 新建与整理
 */
public class DeptAction extends StdSwitchAction implements ZmrConstants
{
    @Override
    protected void validateId(HttpRequest request)
    {
        request.addValidate(new IsInteger("deptId", "请选择一个部门"));
    }

    @Override
    protected void validateForm(HttpRequest request)
    {
        request.addValidate(new IsInteger("deptId", "请选择一个部门"));
        request.addValidate(new IsLen("deptName", "部门名称不能为空且不能超过32个汉字", 1, 32));
        request.addValidate(new IsIntegerValue("deptSeq", "部门排序必须是[0, 999999]范围的非负整数", 0, 999999));
        request.addValidate(new IsLen("deptDesc", "部门描述可为空或不能超过100个汉字", 0, 100));
    }

    @Override
    protected void list(HttpRequest request) throws Exception
    {
        request.setAttribute("tree", ZmrDeptDao.tree());
    }
    
    @Override
    protected void add(HttpRequest request) throws Exception
    {
        long deptId = request.getParameterLong("deptId");
        if (deptId == -1)
        {
            request.returnHistory("请选择一个部门");
            return;
        }
        
        ZmrDept parent = ORM.table().item(ZmrDept.class, deptId);
        if (parent == null)
        {
            request.returnHistory("请选择一个有效的部门");
            return;
        }
        
        request.setAttribute("parent", parent);
    }

    @Override
    protected void modify(HttpRequest request) throws Exception
    {
        long deptId = request.getParameterLong("deptId");
        ZmrDept item = ORM.table().item(ZmrDept.class, deptId);
        if (item == null)
        {
            request.returnHistory("请选择一个有效的部门");
            return;
        }
        
        request.setAttribute("item", item);
    }

    @Override
    protected void insert(HttpRequest request) throws Exception
    {
        ZmrDept item = request.getParameter(ZmrDept.class);
        ZmrDept parent = ORM.table().item(ZmrDept.class, item.getParentId());
        if (parent == null || !parent.isDeptValid())
        {
            request.returnHistory("请选择一个有效的上级部门部门");
            return;
        }
        
        item.setDeptLevel(parent.getDeptLevel()+1);
        if (parent.getDeptLevel() == 0)
            item.setDeptParentAll(""+parent.getDeptId());
        else
            item.setDeptParentAll(parent.getDeptParentAll() + "," + parent.getDeptId());
        
        ORM.table().insert(item);
        
        //修改负责人所属部门
        List<String> operatorCodeList = new ArrayList<String>();
        if (Validates.isNotEmptyBlank(item.getDeptManager()))
            operatorCodeList.add(item.getDeptManager());
        if (Validates.isNotEmptyBlank(item.getDeptSubManager()))
            operatorCodeList.addAll(Lists.toStringList(item.getDeptSubManager()));
        
        ZmrDeptDao.doUpdateDeptManeger(request, operatorCodeList, item.getDeptId()+"", item.getDeptParentAll());
        
        //增加操作日志
        ZmrOperatorDao.addOperateLog(request, "增加部门", Jsons.toString(item));
    }

    @Override
    protected void update(HttpRequest request) throws Exception
    {
        ZmrDept item = request.getParameter(ZmrDept.class);
        ORM.table().update(item);
        
        //修改负责人所属部门
        List<String> operatorCodeList = new ArrayList<String>();
        if(Validates.isNotEmptyBlank(item.getDeptManager()))
            operatorCodeList.add(item.getDeptManager());
        if(Validates.isNotEmptyBlank(item.getDeptSubManager()))
            operatorCodeList.addAll(Lists.toStringList(item.getDeptSubManager()));
        
        ZmrDeptDao.doUpdateDeptManeger(request, operatorCodeList, item.getDeptId()+"", item.getDeptParentAll());
        
        //增加操作日志
        ZmrOperatorDao.addOperateLog(request, "修改部门", Jsons.toString(item));
    }

    @Override
    protected void delete(HttpRequest request) throws Exception
    {
        long deptId = request.getParameterLong("deptId");
        ZmrDept item = ORM.table().item(ZmrDept.class, deptId);
        if (item == null)
        {
            request.returnHistory("请选择一个有效的部门");
            return;
        }
        
        if (item.getDeptLevel() == 0)
        {
            request.returnHistory("根部门不能删除，只允许修改");
            return;
        }
        
        if (ORM.table().count(ZmrDept.class, new Selector("parentId", deptId)) > 0)
        {
            request.returnHistory("该部门有下级部门，要先删除下级部门才能删除本部门");
            return;
        }
        
        if (ORM.table().count(ZmrOperator.class, new Selector().addMustLike("operatorDept", deptId)) > 0)
        {
            request.returnHistory("该部门已有操作员，请删除部门成员后再删除本部门");
            return;
        }
        
        ORM.table().delete(ZmrDept.class, deptId);
        
        //增加操作日志
        ZmrOperatorDao.addOperateLog(request, "删除部门", Jsons.toString("deptId", deptId));
    }
}